﻿package hejie.admin.service.impl;

import java.util.List;

import hejie.admin.dao.EmployeeDao;
import hejie.admin.service.EmployeeService;


import javax.annotation.Resource;
import org.springframework.stereotype.Component;



import publics.model.Employee;
import publics.util.Page;


@Component("employeeService")
public class EmployeeServiceImpl implements EmployeeService {


	EmployeeDao employeeDao;
	
	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}	
	@Resource(name="employeeDao")
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
		System.out.println("employeeDaoImpl inject in EmployeeServiceImpl");
	}

	public Object saveEmployee(Employee employee) {
		List<Employee> staffNos = employeeDao.findByStaffNo(employee.getStaffNo());	
		if(staffNos.size() == 0){
			return employeeDao.save(employee);		
		}
		else 			
		   return null;
	}

	public Page findByPage(Page page) {
		String sql = "from Employee e";		 
		if(page.getConditions().size() > 0){//这里为模糊查询的 sql 语句拼接
			sql = sql + " where ";  
			for(int i = 0; i < page.getConditions().size(); ){
				sql = sql + "( e.name like '%"+page.getConditions().get(i)+"%'"
				+" or e.staffNo like '%"+page.getConditions().get(i)+"%'"
				+" or e.name like '%"+page.getConditions().get(i)+"%'"
				+" or e.sex like '%"+page.getConditions().get(i)+"%'"
				+" or e.position like '%"+page.getConditions().get(i)+"%'"
				+" or e.phone like '%"+page.getConditions().get(i)+"%'"
				+" or e.email like '%"+page.getConditions().get(i)+"%' ) ";
				//每次上面七个属性只有匹配到的才能取到值，不然为%
				i++;
				if(i < page.getConditions().size()){// 如果条件不止一个 进行连续拼接
					sql = sql + " and ";
				}
			}
		}
		System.out.println("sql ="+sql);
		
		int pageSize = page.getLimit();
		int offset = page.getStart();//-1)*pageSize;
		page.setRoot(employeeDao.findByPage(sql,offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(employeeDao.findByCount(sql));   //把数据总数放入totalProperty中
		return page;
	}

	public boolean updateEmployee(Employee employee) {
		return employeeDao.update(employee);

	}

	public boolean deleteEmployee(Integer id) {
		System.out.println("service里 employeeId="+id);
		return employeeDao.deleteById(id);
	}
	/* (non-Javadoc)
	 * @see hejie.admin.service.EmployeeService#findByUser(java.lang.String)
	 */
	public Employee findByUser(String user) {
		//未实现，请补充！
		return null;
	} 	
	
	
	
	
}
